package cn.smy.dama2;

import cn.data.collection.constant.BusConstant;
import cn.data.collection.constant.CommonData;
import cn.data.collection.constant.InitPropertiesConstant;
import cn.data.collection.constant.NumberConstant;
import cn.data.collection.exception.CollectionException;
import cn.data.collection.util.PropertiesUtils;

public class CrackValidateCode {
	private Dama2 dama2 = new Dama2();

	private CrackValidateCode() {
	}

	// 注意，这里没有final
	private static CrackValidateCode single = null;

	// 静态工厂方法
	public static CrackValidateCode getInstance() {
		if (single == null) {
			single = new CrackValidateCode();
		}
		return single;
	}

	private int init() {
		return dama2.init("企业名称采集", "cb9217191ec0d2e84767079196ed4173");
	}

	public int loginIn() {
		int initRet = this.init();
		if (NumberConstant.ZERO != initRet) {
			return initRet;
		}
		String[] retSysAnnouncement = new String[1];
		String[] retAppAnnouncement = new String[1];
		int ret = dama2.login("test", "test", "", retSysAnnouncement, retAppAnnouncement);
		if (NumberConstant.ZERO != ret) {
			return initRet;
		}
		return 0;
	}

	public void logOut() {
		dama2.logoff();
	}

	public String getValidateCode(String validateCodeName) throws Exception {
		String[] vcode = new String[1];
		int ret = dama2.d2File(CommonData.DAMA_SOFTWARE_ID, // software ID
				CommonData.DAMA_USER_NAME, // user name
				CommonData.DAMA_PASSWORD, // user password
				PropertiesUtils.getInstance().getINITDATA().getProperty(InitPropertiesConstant.DAMA_IMG_LOCATION)+"/"+validateCodeName+".png",// file name
				(short) 40, // timeout , seconds
				(long) 72, // code type id
				vcode); // return code text
		if (ret > 0) {
			return vcode[0];
		}else{
			throw new CollectionException(BusConstant.B_80005.getCode(),"错误代码:"+ret);
		}
	}

	public static void main(String[] args) throws Exception {
//		if (CrackValidateCode.getInstance().loginIn() == 0) {
			CommonData.DAMA_SOFTWARE_ID = "cbf7d01368041ac3127df4aaae0e73c7";
			CommonData.DAMA_USER_NAME = "test";
			CommonData.DAMA_PASSWORD = "test";
			System.out.println(PropertiesUtils.getInstance().getINITDATA().getProperty(InitPropertiesConstant.DAMA_IMG_LOCATION));
			System.out.println(CrackValidateCode.getInstance().getValidateCode("33538691694824596.png"));
//		}
		// CrackValidateCode.getInstance().logOut();
	}

}
